package com.zhuss.product.processor;

import java.util.*;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import com.zhuss.bean.bo.product.product.CreateProductBo;
import com.zhuss.common.AbstractProcessor;
import com.zhuss.common.Processor;
import com.zhuss.product.db.dao.ext.ProductDao;
import com.zhuss.product.db.entity.Product;
import com.zhuss.db.entity.OpLog;
import com.zhuss.enums.LogActionEnum;
import com.zhuss.enums.MoudleEnum;
import com.zhuss.exception.OceanException;
import com.zhuss.util.JsonUtil;


/**
 * @description: 商品创建
 * @author: zhushanshuo
 * @create: Wed Mar 24 22:26:45 CST 2021
 **/
@Component("productCreate")
public class ProductCreate extends AbstractProcessor<Object, CreateProductBo> implements Processor<Object, CreateProductBo> {

    @Autowired
    private ProductDao productDao;
    
    @Autowired
	@Qualifier("productRead")
	private ProductRead productRead;
    
    
    /**
     * 校验
     */
    @Override
    public void check(CreateProductBo bo) throws OceanException {
    	// TODO 创建 商品 校验， 请补充
    	String name = bo.getName();
    	if (name == null || "".equals(name.trim())) {
    		// throw new OceanException(ExceptionCode.商品name为空);
    	}
    	
    	List<Product> list = productRead.list(name);
    	if (!list.isEmpty()) {
    		// throw new OceanException(ExceptionCode.商品name已存在);
    	}
    }
    
    /**
     * 操作方法
     */
    @Override
    public Object process(CreateProductBo param) {
    	Product record = new Product();
    	record.setProductNo(param.getProductNo());
    	record.setCompanyId(param.getCompanyId());
    	record.setName(param.getName());
    	record.setDate(param.getDate());
    	record.setStartTime(param.getStartTime());
    	record.setEndTime(param.getEndTime());
    	record.setStatus(param.getStatus());
    	record.setPrice(param.getPrice());
    	record.setTotal(param.getTotal());
    	record.setUsed(param.getUsed());
    	record.setSort(param.getSort());

    	
    	record.setCreatorId(param.getConcurrentUserId());
    	record.setCreator(param.getConcurrentUserName());
    	record.setCreateTime(new Date());
    	productDao.create(record);
    	param.setTargetId(record.getProductId());
        return null;
    }
    
    /**
     * 操作日志
     */
	@Override
	public Object log(CreateProductBo param) throws OceanException {
		OpLog record = new OpLog();
    	record.setMoudle(MoudleEnum.商品模块.key());
    	record.setAction(LogActionEnum.商品创建.key());
    	record.setTargetId(param.getTargetId());
    	record.setTargetName(param.getName());
    	record.setContent("参数: " + JsonUtil.toJson(param));
    	record.setReason(param.getExceptionReason());
    	
    	record.setCreatorId(param.getConcurrentUserId());
    	record.setCreator(param.getConcurrentUserName());
    	record.setCreateTime(new Date());
    	log(record);
    	return null;
	}
}
